package com.tianyang.yukuaifu.business.main.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseTitleActivity;
import com.tianyang.yukuaifu.business.main.adapter.CodePlateListAdapter;
import com.tianyang.yukuaifu.business.main.bean.DeviceOutput;
import com.tianyang.yukuaifu.business.main.contract.CodePlateListContract;
import com.tianyang.yukuaifu.business.main.contract.MainConfig;
import com.tianyang.yukuaifu.business.main.presenter.CodePlateListPresenter;
import com.tianyang.yukuaifu.business.main.provider.MainOpenProvider;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.jump.eventbus.RefreshDeviceEvent;
import com.tianyang.yukuaifu.module.dialog.RemindDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.TwoBtnClickListener;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：码牌/音响列表
 * Created by xyj on 2024/4/19
 */
public class CodePlateListActivity extends BaseTitleActivity implements CodePlateListContract.View {

    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.confirmTv)
    Button confirmTv;

    private CodePlateListAdapter vCodePlateListAdapter;

    private CodePlateListContract.Presenter mPresenter;

    private String deviceType, relationId;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_code_plate_list;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView() {
        super.initView();
        deviceType = getIntent().getStringExtra(MainConfig.INTENT_DEVICE_TYPE);
        String title = getIntent().getStringExtra(MainConfig.INTENT_TITLE);
        if (!TextUtils.isEmpty(title)) {
            commonTitle.setTitle(title + "列表");
            confirmTv.setText("新增" + title);
        } else {
            commonTitle.setTitle("列表");
            confirmTv.setText("新增");
        }
        vCodePlateListAdapter = new CodePlateListAdapter(deviceType);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(vCodePlateListAdapter);

        mPresenter = new CodePlateListPresenter(this, deviceType);
        mPresenter.getDeviceList();
    }

    @Override
    protected void setListener() {
        super.setListener();
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.getDeviceList();
            }
        });
        vCodePlateListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                DeviceOutput output = ((CodePlateListAdapter)adapter).getItem(position);
                if (output == null) return;
                switch (view.getId()) {
                    case R.id.modifyRtv:
                        MainOpenProvider.getInstance().openModifyDeviceActivity(mContext, output.getId(), output.getPosName());
                        break;
                    case R.id.relationRtv:
                        relationId = output.getId();
                        MainOpenProvider.getInstance().openChooseSoundActivity(mContext, CommonConfig.REQUESTCODE_CHOOSE_SOUND);
                        break;
                    case R.id.unRelationRtv:
                        new RemindDialog(mContext)
                                .content("确认设备《"+output.getPosName()+"》解绑音响？")
                                .cancel("我再想想")
                                .comfirm("立即解绑")
                                .listener(new TwoBtnClickListener() {
                                    @Override
                                    public void onLeftClick() {

                                    }

                                    @Override
                                    public void onRightClick() {
                                        mPresenter.unRelationDevice(output.getId());
                                    }
                                }).show();

                        break;
                    case R.id.takeCodeRtv:
                        MainOpenProvider.getInstance().openMyCodePlateActivity(mContext, output.getImageUrl(), output.getPosName());
                        break;
                }
            }
        });
    }

    @OnClick({R.id.confirmTv})
    public void onClick(View view) {
        if (view.getId() == R.id.confirmTv) {
            MainOpenProvider.getInstance().openBindCodePlateActivity(getContext(), deviceType, confirmTv.getText().toString());
        }
    }

    @Override
    public void showDeviceListView(List<DeviceOutput> outputs) {
        if (srl.isRefreshing())
            srl.finishRefresh();
        if (outputs != null) {
            vCodePlateListAdapter.setNewData(outputs);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshDeviceEvent(RefreshDeviceEvent event) {
        if (event != null) {
            mPresenter.getDeviceList();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CommonConfig.REQUESTCODE_CHOOSE_SOUND && resultCode == RESULT_OK && data != null) {
            DeviceOutput output = (DeviceOutput) data.getSerializableExtra(MainConfig.INTENT_DEVICE_OUTPUT);
            if (output != null) {
                mPresenter.relationDevice(relationId, output.getPosNumber());
            }
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }

    @Override
    public void setPresenter(CodePlateListContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
